/**
 * Copyright (c) 2006 RiseSoft Co.,Ltd  
 * $Header$
 */
package net.risesoft.risebi.collection.web.admin;


import java.util.List;
import java.util.Map;

import net.risesoft.risebi.collection.commons.constants.SystemConstants;
import net.risesoft.risebi.collection.model.Service;
import net.risesoft.risebi.collection.model.Speciality;
import net.risesoft.risebi.collection.model.User;
import net.risesoft.risebi.collection.service.ApplicationManager;
import net.risesoft.risebi.collection.service.ServiceManager;
import net.risesoft.risebi.collection.service.SpecialityManager;
import net.risesoft.risebi.collection.web.BaseAction;
import net.risesoft.risebi.commons.struts2extension.Trimable;
import net.risesoft.risebi.commons.util.GUID;
import net.risesoft.risebi.commons.util.JSONUtil;
import net.risesoft.risebi.commons.util.PropsUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.beanutils.LazyDynaBean;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;

/**
 * 专业管理action
 *
 * @author <a href="mailto: dingzhaojun@risesoft.net">dingzhaojun</a>
 * @version $Revision$
 * 
 */
@Namespace("/admin/SpecialityAction")
public class SpecialityAction extends BaseAction implements Trimable {	
	private static final long serialVersionUID = 3697650986750286102L;
	private static Log log = LogFactory.getLog(SpecialityAction.class);
	
	private SpecialityManager specialityManager;
	private ApplicationManager applicationManager;
	private ServiceManager serviceManager;
	private String json_services;	
	private String currentServiceGuid;	
	
	@Action(value="prepareSpecialityList",
			results={@Result(name="success", location="/admin/speciality/specialityList.jsp")}
	)
	public String prepareSpecialityList() throws Exception {
		this.currentServiceGuid = this.getDefaultService().getGuid();	
		
		List<Service> lst = serviceManager.getObjects(Service.class);
		this.json_services = JSONUtil.getJsonString("include","guid,name",lst);
		
		return SUCCESS;
	}
	
	/**
	 * @see net.risesoft.commons.struts2extension.Trimable#afterTrim(com.opensymphony.xwork2.ActionInvocation)
	 */
	public void afterTrim(ActionInvocation invocation) {
		
	}

	/**
	 * @see net.risesoft.commons.struts2extension.Trimable#preTrim(com.opensymphony.xwork2.ActionInvocation)
	 */
	public boolean preTrim(ActionInvocation invocation) {
		return true;
	}
	
	@Action(value="refreshJson")
	public String refreshJson() throws Exception {
		try{
			if("jsonList".equalsIgnoreCase(this.whichJson)){
				this.currentServiceGuid = this.getDefaultService().getGuid();	
				
				List<Speciality> lst = specialityManager.find("from Speciality s where s.service.guid='" + this.currentServiceGuid + "'");
				this.json = JSONUtil.getJsonString("include","serviceGuid,guid,name,chineseName,description,tabIndex",lst);
			}
		}catch(Exception e){
			this.json = "[]";
			e.printStackTrace();
		} 

		return AJAX;
	}

	@Action(value="insertOrUpdate")
	public String insertOrUpdate() throws Exception {
		LazyDynaBean ldb = new  LazyDynaBean();
		try{
			Speciality[] specialityList = (Speciality[])JSONUtil.stringToArray(json, Speciality.class);
			for (Speciality s : specialityList){
				if(s.getGuid()==null || s.getGuid().trim().length()==0 || s.getGuid().startsWith("NEW")){
					s.setGuid(new GUID().toString());
				}
				//应该排重
				specialityManager.saveObject(s);
			}
			
			ldb.set("success", true);
			ldb.set("reason", "保存成功!");
			this.json = JSONObject.fromObject(ldb).toString();
		}catch(Exception e){
			ldb.set("success", false);
			ldb.set("reason", e.getMessage());
			this.json = JSONObject.fromObject(ldb).toString();
			e.printStackTrace();
		}

		return AJAX;
	}

	@Action(value="remove")
	public String remove() throws Exception {
		LazyDynaBean ldb = new  LazyDynaBean();
		try{
			specialityManager.removeObject(Speciality.class, node);
			
			ldb.set("success", true);
			ldb.set("reason", "删除专业成功!");
			this.json = JSONObject.fromObject(ldb).toString();
		}catch(Exception e){
			ldb.set("success", false);
			ldb.set("reason", e.getMessage());
			this.json = JSONObject.fromObject(ldb).toString();
			e.printStackTrace();
		}
		
		return AJAX;		
	}

	public SpecialityManager getSpecialityManager() {
		return specialityManager;
	}

	public void setSpecialityManager(SpecialityManager specialityManager) {
		this.specialityManager = specialityManager;
	}

	public ApplicationManager getApplicationManager() {
		return applicationManager;
	}

	public void setApplicationManager(ApplicationManager applicationManager) {
		this.applicationManager = applicationManager;
	}

	public String getJson_services() {
		return json_services;
	}

	public void setJson_services(String json_services) {
		this.json_services = json_services;
	}
	
	public String getCurrentServiceGuid() {
		return currentServiceGuid;
	}

	public void setCurrentServiceGuid(String currentServiceGuid) {
		this.currentServiceGuid = currentServiceGuid;
	}

	public ServiceManager getServiceManager() {
		return serviceManager;
	}

	public void setServiceManager(ServiceManager serviceManager) {
		this.serviceManager = serviceManager;
	}

}
